package kava.framework.lcdui;

import java.util.Hashtable;

import javax.microedition.lcdui.Font;

import kava.framework.util.Util;

public class ScriptParser {
	public static void parseFormFromScript(Form form, String res, String ID) {
		String beginLine = ID + " form begin";
		String script = Util.readUTFFile(res, form.getClass());
		if (script == null) {
			return;
		}
		String[] lines = Util.split(script, "\n");
		StringBuffer sb = new StringBuffer();
		int beginIndex = -1;
		for (int i = 0; i < lines.length; i++) {
			String line = lines[i].trim();
			if (line.length() < 1 || line.startsWith("//")) {
				continue;
			} else if (line.startsWith(beginLine)) {
				beginIndex = i;
				break;
			}
		}
		if (beginIndex != -1) {
			for (int i = beginIndex + 1; i < lines.length; i++) {
				String line = lines[i].trim();
				if (line.length() < 1 || line.startsWith("//")) {
					continue;
				} else if (line.endsWith("form end")) {
					break;
				} else {
					sb.append(line + "\n");
				}
			}
		}
		if (sb.length() > 0) {
			parse(form, sb.toString());
		}

	}

	private static void parse(Form form, String script) {
		String[] lines = Util.split(script, "\n");
		for (int i = 0; i < lines.length;) {
			String line = lines[i].trim();
			if (line.length() < 1) {
				i++;
				continue;
			}
			if (line.startsWith("x")) {
				int pos = line.indexOf('=');
				String value = line.substring(pos + 1).trim();
				form.setX(Integer.parseInt(value));
				i++;
			} else if (line.startsWith("y")) {
				int pos = line.indexOf('=');
				String value = line.substring(pos + 1).trim();
				form.setY(Integer.parseInt(value));
				i++;
			} else if (line.startsWith("width")) {
				int pos = line.indexOf('=');
				String value = line.substring(pos + 1).trim();
				if (value.equals("SW")) {
					form.setWidth(KavaFrame.getScreenWidth());
				} else {
					form.setWidth(Integer.parseInt(value));
				}
				i++;
			} else if (line.startsWith("height")) {
				int pos = line.indexOf('=');
				String value = line.substring(pos + 1).trim();
				if (value.equals("SH")) {
					form.setHeight(KavaFrame.getScreenHeight());
				} else {
					form.setHeight(Integer.parseInt(value));
				}
				i++;
			} else if (line.startsWith("title")) {
				int pos = line.indexOf('=');
				String value = line.substring(pos + 1).trim();
				form.setTitle(value);
				i++;
			} else if (line.startsWith("bgColor")) {
				int pos = line.indexOf('=');
				String value = line.substring(pos + 1).trim();
				form.setBgColor(Util.parseInt(value));
				i++;
			}else if (line.startsWith("pngs")) {
				int pos = line.indexOf('=');
				String value = line.substring(pos + 1).trim();
				form.setPngResStr(value);
				i++;
			} else if (line.startsWith("fontSize")) {
				int pos = line.indexOf('=');
				String value = line.substring(pos + 1).trim();
				int fontSize = Integer.parseInt(value);
				switch (fontSize) {
				case 0:
					form.setFontSize(Font.SIZE_SMALL);
					break;
				case 1:
					form.setFontSize(Font.SIZE_MEDIUM);
					break;
				case 2:
					form.setFontSize(Font.SIZE_LARGE);
					break;
				}
				i++;
			} else if (line.startsWith("fgColor")) {
				int pos = line.indexOf('=');
				String value = line.substring(pos + 1).trim();
				form.setFgColor(Util.parseInt(value));
				i++;
			} else if (line.startsWith("bgImage")) {
				int pos = line.indexOf('=');
				String value = line.substring(pos + 1).trim();
				form.setBgImage(value);
				i++;
			} else if (line.startsWith("isModal")) {
				int pos = line.indexOf('=');
				String value = line.substring(pos + 1).trim();
				form.setModal(value.equals("true"));
				i++;
			} else if (line.startsWith("widgets begin")) {
				while (true) {
					i++;
					if (i >= lines.length) {
						break;
					}
					String next = lines[i].trim();
					if (next.startsWith("widgets end")) {
						break;
					} else {
						ContainerForm cf = (ContainerForm) form;
						Widget child = parseWidget(next,cf);
						cf.addWidget(child);
					}
				}

			} else {
				i++;
			}
		}
	}

	private static Widget parseWidget(String script,ContainerForm parent) {
		String[] fields = Util.split(script, ",");
		Widget widget = new Widget(parent);
		for (int i = 0; i < fields.length; i++) {
			String field = fields[i].trim();
			if (field.length() < 1) {
				continue;
			}
			if (field.startsWith("id")) {
				int pos = field.indexOf('=');
				String value = field.substring(pos + 1).trim();
				widget.ID = Integer.parseInt(value);
			} else if (field.startsWith("x")) {
				int pos = field.indexOf('=');
				String value = field.substring(pos + 1).trim();
				widget.x = Integer.parseInt(value);
			} else if (field.startsWith("y")) {
				int pos = field.indexOf('=');
				String value = field.substring(pos + 1).trim();
				widget.y = Integer.parseInt(value);

			} else if (field.startsWith("width")) {
				int pos = field.indexOf('=');
				String value = field.substring(pos + 1).trim();
				if (value.equals("SW")) {
					widget.width = (KavaFrame.getScreenWidth());
				} else {
					widget.width = (Integer.parseInt(value));
				}

			} else if (field.startsWith("height")) {
				int pos = field.indexOf('=');
				String value = field.substring(pos + 1).trim();
				if (value.equals("SH")) {
					widget.height = (KavaFrame.getScreenHeight());
				} else {
					widget.height = Integer.parseInt(value);
				}
			} else if (field.startsWith("text")) {
				int pos = field.indexOf('=');
				String value = field.substring(pos + 1).trim();
				widget.text = value;

			} else if (field.startsWith("fontSize")) {
				int pos = field.indexOf('=');
				String value = field.substring(pos + 1).trim();
				int fontSize = Integer.parseInt(value);
				switch (fontSize) {
				case 0:
					widget.fontSize = Font.SIZE_SMALL;
					break;
				case 1:
					widget.fontSize = Font.SIZE_MEDIUM;
					break;
				case 2:
					widget.fontSize = Font.SIZE_LARGE;
					break;
				}

			} else if (field.startsWith("bgColor")) {
				int pos = field.indexOf('=');
				String value = field.substring(pos + 1).trim();
				widget.bgColor = Util.parseInt(value);

			} else if (field.startsWith("bgImage")) {
				int pos = field.indexOf('=');
				String value = field.substring(pos + 1).trim();
				widget.bgImage = value;
			} else if (field.startsWith("fgImage")) {
				int pos = field.indexOf('=');
				String value = field.substring(pos + 1).trim();
				widget.fgImage = value;

			} else if (field.startsWith("fgColor")) {
				int pos = field.indexOf('=');
				String value = field.substring(pos + 1).trim();
				widget.fgColor = Util.parseInt(value);
			} else if (field.startsWith("focusBgColor")) {
				int pos = field.indexOf('=');
				String value = field.substring(pos + 1).trim();
				widget.focusBgColor = Util.parseInt(value);

			} else if (field.startsWith("focusBgImage")) {
				int pos = field.indexOf('=');
				String value = field.substring(pos + 1).trim();
				widget.focusBgImage = value;

			} else if (field.startsWith("focusFgImage")) {
				int pos = field.indexOf('=');
				String value = field.substring(pos + 1).trim();
				widget.focusFgImage = value;

			} else if (field.startsWith("focusFgColor")) {
				int pos = field.indexOf('=');
				String value = field.substring(pos + 1).trim();
				widget.focusFgColor = Util.parseInt(value);

			} else if (field.startsWith("pressedBgColor")) {
				int pos = field.indexOf('=');
				String value = field.substring(pos + 1).trim();
				widget.pressedBgColor = Util.parseInt(value);

			} else if (field.startsWith("pressedBgImage")) {
				int pos = field.indexOf('=');
				String value = field.substring(pos + 1).trim();
				widget.pressedBgImage = value;

			} else if (field.startsWith("pressedFgImage")) {
				int pos = field.indexOf('=');
				String value = field.substring(pos + 1).trim();
				widget.pressedFgImage = value;

			} else if (field.startsWith("pressedFgColor")) {
				int pos = field.indexOf('=');
				String value = field.substring(pos + 1).trim();
				widget.pressedFgColor = Util.parseInt(value);

			} else if (field.startsWith("visual")) {
				int pos = field.indexOf('=');
				String value = field.substring(pos + 1).trim();
				widget.visual = value.equals("true");
			} else if (field.startsWith("state")) {
				int pos = field.indexOf('=');
				String value = field.substring(pos + 1).trim();
				if(value.equals("STATE_FOCUSED")){
					widget.state = Widget.STATE_FOCUSED;
				}else if(value.equals("STATE_PRESSED")){
					widget.state = Widget.STATE_PRESSED;
				}else{
					widget.state = Widget.STATE_NORMAL;
				}
			}
		}
		return widget;
	}

	public static Hashtable getKeyValuePairs(String res, Class classObj,
			boolean lineSep) {
		Hashtable result = new Hashtable();
		String script = Util.readUTFFile(res, classObj);
		if (script != null) {
			String[] pairs = null;
			if (lineSep) {
				pairs = Util.split(script, "\n");
			} else {
				pairs = Util.split(script, "###");
			}
			if (pairs != null && pairs.length > 0) {
				for (int i = 0; i < pairs.length; i++) {
					if (pairs[i] == null || pairs[i].trim().length() < 1
							|| pairs[i].startsWith("//")) {
						continue;
					}
					int pos = pairs[i].indexOf('=');
					if (pos >= 0) {
						String key = pairs[i].substring(0, pos).trim();
						String value = pairs[i].substring(pos + 1).trim();
						result.put(key, value);
					}

				}
			}
			pairs = null;

		}
		script = null;
		System.gc();
		return result;

	}
}
